import { createStore } from "vuex";

export default createStore({
  state: {
    user: null,
    Authorization: localStorage.getItem('Authorization') || null
  },
  mutations: {
    setUser(state, user) {
      state.user = user;
      if (user) {
        localStorage.setItem("user", user);
      } else {
        localStorage.removeItem("user");
      }
    },
    setLoginToken(state, status) {
      state.Authorization = status;
      if (status) {
        localStorage.setItem("Authorization", status);
      } else {
        localStorage.removeItem("Authorization");
      }
    }
  },
  actions: {
    Logins({ commit }, { user, Authorization }) {
      commit("setUser", user);
      commit("setLoginToken", Authorization);
    },
    logout({ commit }) {
      commit("setUser", null);
      commit("setLoginToken", null);
    }
  },
  getters: {
    isLoggedIn: state => !!state.Authorization,
    user: state => state.user
  },
  modules: {}
})
